package com.appkubes.mall.restful.application.user.user.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.core.entity.MallSmsCode;
import com.appkubes.mall.core.entity.MallUser;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.common.sms.entity.vo.CodeVO;
import com.appkubes.mall.restful.application.common.sms.service.CodeService;
import com.appkubes.mall.restful.application.user.user.entity.vo.ModifyUserVo;
import com.appkubes.mall.restful.application.user.user.entity.vo.UserActiveVO;
import com.appkubes.mall.restful.application.user.user.entity.vo.UserRegisterVO;
import com.appkubes.mall.restful.application.user.user.service.UserService;
import com.appkubes.mall.restful.application.user.user.service.UserServiceLogin;
import com.appkubes.mall.restful.application.user.user.service.UserWxAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {


    @Autowired
    UserServiceLogin userServiceLogin;

    @Autowired
    UserWxAppService userWxAppService;

    @Resource
    UserService userService;

    @Resource
    CodeService codeService;

    @ApiOperation("判断用户是否登录")
    @GetMapping("/isActive")
    public Response isActive(UserActiveVO userActiveVO) {
        return Response.success(userService.isActive(userActiveVO));
    }


    @ApiOperation("修改用户信息")
    @PostMapping("/modifyUserInfo")
    public Response modifyUserInfo(@RequestBody @ParamValid ModifyUserVo modifyUserVo) {
        userWxAppService.modifyUserInfo(modifyUserVo);
        userServiceLogin.modifyUserInfo(modifyUserVo);
        return Response.success();
    }


    @ApiOperation("注册用户")
    @PostMapping("/register")
    public Response register(@RequestBody @ParamValid UserRegisterVO registerVO) {

        // 短信验证
        CodeVO codeVO = new CodeVO();
        codeVO.setTel(registerVO.getTel());
        codeVO.setCode(registerVO.getValidateCode());
        MallSmsCode mallSmsCode = codeService.getSmsSurvivor(codeVO);
        if (mallSmsCode == null) {
            return Response.error("验证码过期或错误");
        }
        codeService.smsDie(mallSmsCode);

        MallUser user = userService.register(registerVO);
        return Response.success(user);
    }
}
